package cn.smxy.ln.controller;

import cn.smxy.ln.common.lang.Result;
import cn.smxy.ln.common.utils.RedisUtil;
import cn.smxy.ln.pojo.Work;
import cn.smxy.ln.pojo.WorkContent;
import cn.smxy.ln.pojo.WorkProve;
import cn.smxy.ln.pojo.WorkRegister;
import cn.smxy.ln.service.WorkContentService;
import cn.smxy.ln.service.WorkProveService;
import cn.smxy.ln.service.WorkRegisterService;
import cn.smxy.ln.service.WorkService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ln
 * @date 2022年05月24日 19:46
 */
@RestController
@RequestMapping("/work")
public class WorkController {

    @Autowired
    WorkService workService;

    @Autowired
    WorkContentService workContentService;

    @Autowired
    WorkProveService workProveService;

    @Autowired
    WorkRegisterService workRegisterService;

    @Autowired
    RedisUtil redisUtil;

    @HystrixCommand(fallbackMethod = "hystrixGetList")
    @Cacheable(cacheNames = "work", unless = "#result.getData() == null")    //key:缓存数据时使用的 key，可以用它来指定。默认是使用方法参数的值；cacheNames/value：二者选其一即可，指定缓存组件的名字
    @GetMapping("/list")
    public Result queryByProjectId(@RequestParam(value = "content", required = false) String content, @RequestParam(value = "scoreRule", required = false) String scoreRule, @RequestParam(value = "projectId", required = false) Long projectId) {
        QueryWrapper query = new QueryWrapper();
        if (content != null) {
            query.like("content", content);
        }
        if (scoreRule != null) {
            query.like("score_rule", scoreRule);
        }
        if (projectId != null) {
            query.eq("project_id", projectId);
        }
        List<Work> list = workService.list(query);
        if (list == null) {
            throw new RuntimeException("数据库没有得到查询所需的work数据，或者信息无法找到");
        }
        return Result.succ(list);
    }

    public Result hystrixGetList(String content,  String scoreRule, Long projectId) {
        return Result.fail("没有得到对应的work数据，或者刷新试试");
    }

    @PostMapping("/save")
    @Transactional
    public Result save(@RequestBody Work work) {
        if (work.getProjectId() == null || work.getProjectId().equals(""))
            return Result.fail("projectId不能为空，保存失败");
        boolean flag = workService.save(work);
        if (flag) {
            redisUtil.flushByX("work");
            return Result.succ(work);
        }
        return Result.fail("保存失败");
    }

    @PostMapping("/delete")
    @Transactional
    public Result delete(@RequestParam Long id) {
        List<WorkContent> workContents = workContentService.list();
        List<WorkProve> workProves = workProveService.list();
        List<WorkRegister> workRegisters = workRegisterService.list();
        for (WorkContent t: workContents) {
            if (t.getWorkId() == id) {
                t.setWorkId(Long.valueOf(0));
                workContentService.updateById(t);
            }
        }
        for (WorkProve t: workProves) {
            if (t.getWorkId() == id) {
                t.setWorkId(Long.valueOf(0));
                workProveService.updateById(t);
            }
        }
        for (WorkRegister t: workRegisters) {
            if (t.getWorkId() == id) {
                t.setWorkId(Long.valueOf(0));
                workRegisterService.updateById(t);
            }
        }
        redisUtil.flushByX("workContent", "workProve", "workRegister");
        boolean flag = workService.removeById(id);
        if (flag) {
            redisUtil.flushByX("work");
            return Result.succ("删除成功");
        }
        return Result.fail("删除失败");
    }

    @PostMapping("/update")
    @Transactional
    public Result update(@RequestBody Work work) {
        if (work.getProjectId() == null || work.getProjectId().equals(""))
            return Result.fail("projectId不能为空，更新失败");
        boolean flag = workService.updateById(work);
        if (flag) {
            redisUtil.flushByX("work");
            return Result.succ(work);
        }
        return Result.fail("修改失败");
    }

}
